﻿#region COPYRIGHT© 2009-2012 Phillip Clark. All rights reserved.
// For licensing information see License.txt (MIT style licensing).
#endregion

using System.Globalization;
using FlitBit.Parse;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace FlitBit.Parse.Tests
{
	/// <summary>
	/// Summary description for MultiMatchTests
	/// </summary>
	[TestClass]
	public class MultiMatchTests
	{
		public TestContext TestContext { get; set; }

		[TestMethod]
		public void MatchIdentifier()
		{
			CharRule first = CharRule.MatchAny(CharMatch.Exact('_')
				, CharMatch.UnicodeCategory(UnicodeCategory.LowercaseLetter)
				, CharMatch.UnicodeCategory(UnicodeCategory.UppercaseLetter));
			CharRule subsequent = CharRule.MatchAny(CharMatch.Exact('_')
				, CharMatch.UnicodeCategory(UnicodeCategory.LowercaseLetter)
				, CharMatch.UnicodeCategory(UnicodeCategory.UppercaseLetter)
				, CharMatch.UnicodeCategory(UnicodeCategory.DecimalDigitNumber));
			var rule = Match.MatchExact(first, Match.MatchRepeatedly(subsequent));
			var input = "28894 4---- _it 888-45.XY";

			var c = input.LocateFirst(rule);
			Assert.IsTrue(c.IsValid);
			Assert.AreEqual(12, c.Offset);

			Capture capture;
			Cursor copy = new Cursor(c);
			Assert.IsTrue(rule.TryCapture(input, ref c, out capture));
			Assert.AreEqual(copy, capture.Position);
			Assert.AreEqual("_it", capture.Value);
		}

		[TestMethod]
		public void MatchSingleLineComment()
		{
			var rule = CharRule.SingleLineComment;

			var input = @"match a single comment
											a comment that starts // in the middle of a line
											<that was it.";
			var match = "// in the middle of a line";

			Cursor cursor = new Cursor(0);
			Capture capture = input.CaptureNext(rule, ref cursor);
			Assert.IsTrue(capture.IsValid);
			Assert.AreEqual(rule, capture.Rule);
			Assert.AreEqual(new Cursor(57), capture.Position);
			Assert.AreEqual(match.Length, capture.Length);
			Assert.AreEqual(match, capture.Value);
		}

		[TestMethod]
		public void MatchMultiLineComment()
		{
			var rule = CharRule.MultiLineComment;

			var input = @"match a multiline comment
											a comment that starts /* in the middle of a line
and continues
for a couple
of lines */
											";
			var match = @"/* in the middle of a line
and continues
for a couple
of lines */";

			Cursor cursor = new Cursor(0);
			Capture capture = input.CaptureNext(rule, ref cursor);
			Assert.IsTrue(capture.IsValid);
			Assert.AreEqual(rule, capture.Rule);
			Assert.AreEqual(new Cursor(60), capture.Position);
			Assert.AreEqual(match.Length, capture.Length);
			Assert.AreEqual(match, capture.Value);
		}
	}
}
